package cn.edu.dlmu.cogo.register;

import cn.edu.dlmu.cogo.framework.annotation.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author hongda.li@hand-china.com 2022/3/10 21:10
 */
@Component
public class LocalServiceCachePool {

    private final Map<String, ServiceInstance> cache = new ConcurrentHashMap<>();

    public void setServiceInstance(ServiceInstance serviceInstance){
        this.cache.put(serviceInstance.getServiceName(),serviceInstance);
    }

    /**
     * 默认取出微服务实例并不续期
     * @param serviceName 微服务名称
     * @return 实例
     */
    public ServiceInstance getServiceInstance(String serviceName) {
        return this.cache.get(serviceName);
    }

    public void removeServiceInstance(String serviceName){
        this.cache.remove(serviceName);
    }

    public List<ServiceInstance> getServices() {
        return new ArrayList<>(this.cache.values());
    }
}
